package fenyu.personblob.savepath.strategy;

import fenyu.personblob.bean.databean.logs.LogBean;
import fenyu.personblob.bean.observe.Observe;
import fenyu.personblob.bean.strategy.Strategy;

import java.util.ArrayList;
import java.util.List;

/**
 * create at 2018/12/22 7:25
 *
 * @author 纷羽 <gzj0078@gmail.com>
 */
public class CreateLogStrategy implements Strategy {

    private List<Observe> logObserves = new ArrayList<Observe>();

    public Object todo(Object... args) {
        Integer type;
        String logs;
        if (args.length == 1) {
            type = 0;
            logs = (String) args[0];
        } else if (args[0] instanceof Integer) {
            type = (Integer) args[0];
            logs = (String) args[1];
        } else {
            type = (Integer) args[1];
            logs = (String) args[0];
        }
        LogBean logBean = new LogBean();
        logBean.setType(type);
        logBean.setLog(logs);
        notifyObserves(logBean);
        return logBean;
    }

    public void addObserve(Observe observe) {
        logObserves.add(observe);
    }

    public void removeObserve(Observe observe) {
        logObserves.remove(observe);
    }

    private void notifyObserves(LogBean logBean) {
        for (Observe o : logObserves) {
            o.update(logBean);
        }
    }
}
